<template>
  <el-container class="home-container">
    <el-header>
      <div>
        <img src="../assets/logo.png" />
        <span>后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏内部区域 -->
      <el-aside :width="isCollapse ? '64px' : '200px' ">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-menu
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#409eff"
          :collapse-transition="false"
          :unique-opened="true"
          :router="true"
          :default-active="activePath"
          :collapse="isCollapse"
        >
          <!--一级菜单-->
          <el-submenu :index="item.id +''" :key="item.id" v-for="item in menulist">
            <template slot="title">
              <i :class="iconObj[id]"></i>
              <span>{{item.anthName}}</span>
            </template>
            <!--二级菜单-->path
            <el-submenu :index="'/' + subItem.path" :key="subItem.id" v-for="subItem in item.childer"
            @click="saveNavState('/' + subItem.path)">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{subItem.anthName}}</span>
              </template>
              <!--<el-menu-item index="1-4-1">选项1</el-menu-item>三级菜单-->
            </el-submenu>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <!-- 放置路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menulist: [],
      iconObj: {
        '125': 'iconfont icon-user',
        '103': 'iconfont icon-tijikongjian',
        '101': 'iconfont icon-3702mima',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'
      },
      //是否折叠
      isCollapse: false,
      //被激活的链接地址
      activePath:''
    }
  },
  created() {
    this.getMenulist()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    }
  },
  async getMenulist() {
    const { data: res } = await this.$http.get('menus')
    if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
    this.menulist = res.data
    console.log(res)
  },
  toggleCollapse() {
    this.isCollapse = !this.isCollapse
    //点击按钮切换菜单的折叠与展开
  },
  //保存链接激活状态
  saveNavState(activePath){
    window.sessionStorage.setItem("activePath",activePath)
    this.activePath = activePath
  }
}
</script>

<style lang="scss" scoped>
.home-container {
  height: 100%;
}
.el-header,
.el-footer {
  background-color: #373d41;
  justify-content: space-between;
  color: white;
  padding-left: 0;
  display: flex;
  align-items: center;
  div {
    display: flex;
    align-items: center;
  }
  span {
    margin-left: 15px;
  }
  img {
    width: 50px;
    height: 50px;
  }
}

.el-aside {
  color: #333;
  text-align: center;
  line-height: 200px;
  background: #545c64;
  .el-menu {
    border-right: none;
  }
}

.el-main {
  background-color: #e9eef3;
  color: #333;
  text-align: center;
  line-height: 160px;
}
.iconfont {
  margin-right: 10px;
}
.toggle-button {
  background: #4a5064;
  text-align: center;
  font-size: 10px;
  line-height: 24px;
  text-align: center;
  color: white;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>